package csel.view.status;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.LinkedList;

import javax.swing.*;

import csel.model.*;
import csel.model.items.*;

public class InventoryMenu extends JPanel implements Observer<Object>
{
	private static final long serialVersionUID = 1L;
	
	private ItemCollection<UseableItem> myUseableInv;
	private ItemCollection<EquippableItem> myEquippableInv;
	private ActionListener myListener;
	private final InvMenuManager myManager;
	
	private JScrollPane myScrollPane;
	private JPanel invPanel;
	
	private LinkedList<JLabel> nameSet = new LinkedList<JLabel>();
	private LinkedList<JLabel> descSet = new LinkedList<JLabel>();
	private LinkedList<JButton> dropButtonSet = new LinkedList<JButton>();
	private LinkedList<JButton> useSelfButtonSet = new LinkedList<JButton>();
	private LinkedList<JButton> useOtherButtonSet = new LinkedList<JButton>();
	private LinkedList<JButton> eqButtonSet = new LinkedList<JButton>();
	
	private TakeableItem lastSelected = null;
	
	public static InventoryMenu initialize(ItemCollection<UseableItem> useInv, ItemCollection<EquippableItem> equipInv, ActionListener myListener)
	{
		InventoryMenu theInvMenu = new InventoryMenu(useInv,equipInv,myListener);
		useInv.addObserver(theInvMenu);
		equipInv.addObserver(theInvMenu);
		return theInvMenu;
	}
	
	private InventoryMenu(ItemCollection<UseableItem> useInv, ItemCollection<EquippableItem> equipInv, ActionListener myListener)
	{
		myUseableInv = useInv;
		myEquippableInv = equipInv;
		this.myListener = myListener;
		myManager = new InvMenuManager();
		
		invPanel = new JPanel();
		
		setLayout(new GridBagLayout());
		invPanel.setLayout(new GridBagLayout());
		
		buildInvPanel();
	}
	
	private void buildInvPanel()
	{
		GridBagConstraints c = new GridBagConstraints();
		
		nameSet = new LinkedList<JLabel>();
		descSet = new LinkedList<JLabel>();
		dropButtonSet = new LinkedList<JButton>();
		useSelfButtonSet = new LinkedList<JButton>();
		useOtherButtonSet = new LinkedList<JButton>();
		eqButtonSet = new LinkedList<JButton>();
		
		int useSize = myUseableInv.size();
		int eqSize = myEquippableInv.size();
		int i = 3*(useSize);
		
		for (int a = 0; a < useSize; ++a)
		{
			nameSet.add(textLabel(myUseableInv.getItem(a).getName(),Color.BLACK,12));
			descSet.add(textLabel(myUseableInv.getItem(a).getDescription(),Color.BLUE,12));
			useSelfButtonSet.add(useSelfButton(myUseableInv.getItem(a)));
			useOtherButtonSet.add(useOtherButton(myUseableInv.getItem(a)));
			dropButtonSet.add(dropButton(myUseableInv.getItem(a)));
		}
		for (int a = 0; a < i; a += 3)
		{
			c.gridwidth = 1;
			c.gridy = a;
			c.ipady = 5;
			c.gridx = 0;
			invPanel.add(nameSet.get(a/3),c);
			c.gridy = a+1;
			invPanel.add(descSet.get(a/3),c);
			c.gridy = a+2;
			c.gridx = 0;
			invPanel.add(useSelfButtonSet.get(a/3),c);
			c.gridy = a+2;
			c.gridx = 1;
			invPanel.add(useOtherButtonSet.get(a/3),c);
			c.gridx = 2;
			invPanel.add(dropButtonSet.get(a/3),c);
		}

		nameSet = new LinkedList<JLabel>();
		descSet = new LinkedList<JLabel>();
		dropButtonSet = new LinkedList<JButton>();
		
		i = 3*(eqSize+useSize);
		for (int a = 0; a < eqSize; ++a)
		{
			nameSet.add(textLabel(myEquippableInv.getItem(a).getName(),Color.BLACK,12));
			descSet.add(textLabel(myEquippableInv.getItem(a).getDescription(),Color.BLUE,12));
			eqButtonSet.add(equipButton(myEquippableInv.getItem(a)));
			dropButtonSet.add(dropButton(myEquippableInv.getItem(a)));
		}
		int eqIndex = 0;
		for (int a = 3*useSize; a < i; a+=3)
		{
			c.gridwidth = 1;
			c.gridy = a;
			c.ipady = 5;
			c.gridx = 0;
			invPanel.add(nameSet.get(eqIndex),c);
			c.gridy = a+1;
			invPanel.add(descSet.get(eqIndex),c);
			c.gridy = a+2;
			c.gridx = 0;
			invPanel.add(eqButtonSet.get(eqIndex),c);
			c.gridx = 1;
			invPanel.add(dropButtonSet.get(eqIndex++),c);
		}
		
		myScrollPane = new JScrollPane(invPanel,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
		myScrollPane.setPreferredSize(new Dimension(375,125));

		c.gridx = 0;
		c.gridy = 1;
		add(myScrollPane,c);

		setPreferredSize(new Dimension(375,125));
	}
	
	private JButton dropButton(TakeableItem myItem)
	{
		JButton myButton = new JButton("Drop");
		myButton.setActionCommand("imDrop");
		myButton.putClientProperty("contents",myItem);
		myButton.addActionListener(myManager);
		return myButton;
	}
	
	private JButton equipButton(EquippableItem myEquip)
	{
		JButton myButton = new JButton("Equip");
		myButton.setActionCommand("imEquip");
		myButton.putClientProperty("contents",myEquip);
		myButton.addActionListener(myManager);
		return myButton;
	}
	
	private JButton useSelfButton(UseableItem myItem)
	{
		JButton myButton = new JButton("Use (Self)");
		myButton.setActionCommand("imUseSelf");
		myButton.putClientProperty("contents",myItem);
		myButton.addActionListener(myManager);
		return myButton;
	}
	
	private JButton useOtherButton(UseableItem myItem)
	{
		JButton myButton = new JButton("Use (Other)");
		myButton.setActionCommand("imUseOther");
		myButton.putClientProperty("contents",myItem);
		myButton.addActionListener(myManager);
		return myButton;
	}
	
	private JLabel textLabel(String s, Color c, int size)
	{
		JLabel myLabel = new JLabel(s);
		myLabel.setFont(new Font("Serif", Font.PLAIN, size));
		myLabel.setForeground(c);
		return myLabel;

	}
	
	public void mupdate(Object o, Object param)
	{
		if (o.equals(myUseableInv) || o.equals(myEquippableInv))
		{
			//TODO: fill this in
		}
	}
	
	public TakeableItem getSelectedItem()
	{
		//TODO: implement me!
		return lastSelected;
	}
	
	public void setInventories(ItemCollection<UseableItem> myUseableInv, ItemCollection<EquippableItem> myEquippableInv)
	{
		this.myUseableInv = myUseableInv;
		this.myEquippableInv = myEquippableInv;
		myUseableInv.addObserver(this);
		myEquippableInv.addObserver(this);
	}
	
	private class InvMenuManager implements ActionListener
    {
            public void actionPerformed(ActionEvent e)
            {
            	JButton theButton = ((JButton) e.getSource());
                lastSelected = (TakeableItem) theButton.getClientProperty("contents");
                System.out.println("lastSelected = "+lastSelected.getName());
                myListener.actionPerformed(e);
            }
    }
	
	private void writeObject(java.io.ObjectOutputStream out) throws java.io.IOException
    {
            throw new java.io.NotSerializableException("InventoryMenu should not be serialized.");
    }
    
    private void readObject(java.io.ObjectInputStream in) throws java.io.IOException
    {
            throw new java.io.NotSerializableException("InventoryMenu should not be serialized.");
    }

	
}
